{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 对于io操作来说，多线程和多进程性能差别不大\n",
    "## 1、通过Thread 类实例化\n",
    "\n",
    "import time\n",
    "import threading\n",
    "\n",
    "def get_detail_html(url):\n",
    "    print(\"get detail html started\")\n",
    "    time.sleep(2)\n",
    "    print(\"get detail html end\")\n",
    "    \n",
    "def get_detail_url(url):\n",
    "    print(\"get detail url started\")\n",
    "    time.sleep(4)\n",
    "    print(\"get detail url end\")\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    thread1 = threading.Thread(target=get_detail_html, args=(\"\",))\n",
    "    thread2 = threading.Thread(target=get_detail_url, args=(\"\",))\n",
    "    \n",
    "    ## 设置守护线程（主线程不管子线程是否执行完毕，就退出）\n",
    "#     thread1.setDaemon(True)\n",
    "#     thread1.setDaemon(True)\n",
    "    \n",
    "    start_time = time.time()\n",
    "    thread1.start()\n",
    "    thread2.start()\n",
    "    \n",
    "    ## 线程阻塞\n",
    "    thread1.join()\n",
    "    thread2.join()\n",
    "    \n",
    "    ## 当主线程退出的时候，子线程kill掉\n",
    "    print(\"last time: {}\".format(time.time() - start_time))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "get detail html started\n",
      "get detail url started\n",
      "get detail html end\n",
      "get detail url end\n"
     ]
    }
   ],
   "source": [
    "## 2、通过继承Thread来实现多线程\n",
    "import time\n",
    "import threading\n",
    "\n",
    "class GetDetailHtml(threading.Thread):\n",
    "    def __init__(self, name):\n",
    "        super().__init__(name=name)\n",
    "        \n",
    "    def run(self):\n",
    "        print(\"get detail html started\")\n",
    "        time.sleep(2)\n",
    "        print(\"get detail html end\")\n",
    "    \n",
    "class GetDtailUrl(threading.Thread):\n",
    "    def __init__(self, name):\n",
    "        super().__init__(name=name)\n",
    "        \n",
    "    def run(self):\n",
    "        print(\"get detail url started\")\n",
    "        time.sleep(4)\n",
    "        print(\"get detail url end\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    thread1 = GetDetailHtml(\"get_detail_html\")\n",
    "    thread2 = GetDtailUrl(\"get_detail_url\")\n",
    "    \n",
    "    ## 设置守护线程（主线程不管子线程是否执行完毕，就退出）\n",
    "#     thread1.setDaemon(True)\n",
    "#     thread1.setDaemon(True)\n",
    "    \n",
    "    start_time = time.time()\n",
    "    thread1.start()\n",
    "    thread2.start()\n",
    "    \n",
    "    ## 线程阻塞\n",
    "    thread1.join()\n",
    "    thread2.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
